package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.system.ApplicationDomain;
	import flash.utils.setTimeout;
	
	import org.hamcrest.mxml.collection.InArray;
	
	import ricky.game.Actor;
	import ricky.quadtree.Leaf;
	import ricky.quadtree.Morton;
	import ricky.scene.Camera;
	import ricky.scene.MapData;
	import ricky.scene.MapLayout;
	import ricky.scene.Scene;
	import ricky.utils.FrameManager;
	
	import rickyGame.events.SceneEvent;
	import rickyGame.game.scene.Pathfinder;
	import rickyGame.game.scene.SceneManager;
	
	import test.stage3dMaplayer;
	
	public class maptest extends Sprite
	{
		private var scene:Scene;
		public function maptest()
		{
//			stage.frameRate=60;
//			JDebug.monitor(this.stage);
			this.stage.scaleMode=flash.display.StageScaleMode.NO_SCALE;

			this.stage.align=StageAlign.TOP_LEFT;
			
			super();

			var stage3DAvailable:Boolean = 	ApplicationDomain.currentDomain.hasDefinition("flash.display.Stage3D");
			
 			trace(stage3DAvailable);
			

			m=new stage3dMaplayer();
			this.addChild(m);
//			
			this.graphics.beginFill(0xff000f,.9);
			this.graphics.lineStyle(1,0xff0f00);
//			
			this.graphics.drawCircle(this.stage.stageWidth/2,this.stage.stageHeight/2,4);
			this.graphics.moveTo(0,this.stage.stageHeight/2);
			this.graphics.lineTo(this.stage.stageWidth,this.stage.stageHeight/2);
			graphics.moveTo(stage.stageWidth/2,0);
			graphics.lineTo(stage.stageWidth/2,stage.stageHeight);
			this.graphics.endFill();
//
			this.addEventListener(Event.ENTER_FRAME,onenterFrame);
		}
		var m:stage3dMaplayer
		private function onenterFrame(e:Event):void
		{
			m.render();
		}
		
		var s:SceneManager=new SceneManager();
		private function onSceneLoaded(e:SceneEvent):void
		{
			e.scene.camera.width=this.stage.stageWidth;
			e.scene.camera.height=this.stage.stageHeight;
			//e.scene.map.mouseClick=InputController.instance.mapClick;
			
//			p=new AniObject();
//			p.camera=e.scene.camera;
//			p.graphic.wearClothes("model/role/lms001");
//			s.addGameObject(p);
//			p.speed.base=220;
//			p.X=300;
//			p.Y=200;
//			p.addAct(new MoveAction(p,new Point(300,500)));
			camera=e.scene.camera;// // .camera;
//			camera.offMoving(p.X,p.Y);
//			p.onMovingFun=onMoving;
			
			
			e.scene.camera.offMoveTo(200,300);
			
			addChild(new Stats());
			trace(e.scene.tree);
			var m:uint;
			m=Morton.morton(30,40);
			var begin:Leaf=s.currScene.tree.tree[368];
			m=Morton.morton(90,140);
			var end:Leaf=s.currScene.tree.tree[997];

//			var leaf:Array=Pathfinder.findPath1(begin,end,connectedNodeFunction);
//			
//			trace(leaf);
			
//			setTimeout(addMonster,2000);
		}
		
		private function connectedNodeFunction(node:Leaf):Array
		{
			var array:Array=s.currScene.tree.getAroundLeaf(node.x,node.y,1);
			return array;
		}
		
		var p:Actor;
		
		private function addMonster():void
		{
//			for(var i:int=0;i<200;i++)
//			{
//				var tp:AniObject=new AniObject();
//				tp.camera=s.currScene.camera;//.scene.camera;
//				
//				if(i % 6==0)
//					tp.graphic.wearClothes("model/role/lxs002");
//				else if(i % 5==0)
//					tp.graphic.wearClothes("model/role/lzs003");
//				else if(i % 4==0)
//					tp.graphic.wearClothes("model/role/mms001");
//				else if(i % 3==0)
//					tp.graphic.wearClothes("model/role/lms001");
//				else if(i % 2==0)
//					tp.graphic.wearClothes("model/role/mxs002");
//				else if(i % 1==0)
//					tp.graphic.wearClothes("model/role/mzs003");
//				
//				tp.X=300+Math.random()*200;
//				tp.Y=200+Math.random()*300;;
//				tp.addAct(new MoveAction(tp,new Point(tp.x+1000*Math.random(),tp.x+300*Math.random())));
//				s.addGameObject(tp);
//			}
		}
		
		private function onMoving(p:Actor):void
		{
			camera.offMoving(p.graphic.x,p.graphic.y);
//			s.depthSorting();
		}
		
		private function onMapLoaded():void
		{
//			this.removeChild(scene.loadingView);
//			this.addChild(scene.map);
			
			camera=scene.camera;
			camera.resize(this.stage.stageWidth,this.stage.stageHeight);
//			camera.moveTo(0,0);
		}
		
		private function onEnterFrame(e:Event):void
		{
			if(camera)
			{
//			camera.moving(camera.x+8,camera.y+8);
//			JDebug.trace(camera.x+""+camera.y);
			}
			//JDebug.traceObj(camera);
		}
		
		private function onClick(e:MouseEvent):void
		{

			var x:Number=e.stageX;
			var y:Number=e.stageY;
			
			var p1:Point=new Point(x+camera.x,y+camera.y);
//			if(p)
//			{
//				p.addAct(new MoveAction(p,p1));
//			}
			camera.moveTo(p1.x,p1.y);
//			if(camera)
//			{
//				camera.offMoveTo(x+camera.x,y+camera.y);
//				JDebug.trace(camera.x+""+camera.y);
//			}
			//JDebug.traceObj(camera);
		}
		
		var camera:Camera;
	}
}